﻿using DataAccess;
using Entity;
using System.Data;

namespace Business
{
    public class AdminsBusiness
    {
        public DataTable GetAllData()
        {
            DataTable dt = null;
            string query = "select * from admins";
            dt = DA.GetDataTable(query, CommandType.Text, null, null);
            return dt;
        }

        public DataTable GetPageData(int pageNum)
        {
            DataTable dt = null;
            string query = "select * from admins order by adminid offset @pageNum rows fetch next 10 rows only";
            string[] paras = { "@pageNum" };
            object[] values = { pageNum };
            dt = DA.GetDataTable(query, CommandType.Text, paras, values);
            return dt;
        }

        public DataTable SearchAllData(AdminsEntity ae)
        {
            DataTable dt = null;
            string query = "select * from admins where account like @account";
            string[] paras = { "@account" };
            object[] values = { "%" + ae.Account + "%" };
            dt = DA.GetDataTable(query, CommandType.Text, paras, values);
            return dt;
        }

        public int AddAdmin(AdminsEntity ae)
        {
            int res = 0;
            string query = "insert into admins values (@account,@password,@telephone)";
            string[] paras = { "@account", "@password", "@telephone" };
            object[] values = { ae.Account, ae.Password, ae.Telephone };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }

        public int EditAdmin(AdminsEntity ae)
        {
            int res = 0;
            string query = "update admins set account=@account,password=@password,telephone=@telephone where adminid=@adminid";
            string[] paras = { "@account", "@password", "@telephone", "@adminid" };
            object[] values = { ae.Account, ae.Password, ae.Telephone, ae.Adminid };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }

        public int DeleteAdmin(AdminsEntity ae)
        {
            int res = 0;
            string query = "delete from admins where adminid = @adminid";
            string[] paras = { "@adminid" };
            object[] values = { ae.Adminid };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }

        public object GetPasswordByAccount(AdminsEntity ae)
        {
            object res = null;
            string query = "select password from admins where account = @account";
            string[] paras = { "@account" };
            object[] values = { ae.Account };
            res = DA.GetOneData(query, CommandType.Text, paras, values);
            return res;
        }
    }
}
